//
//  HWStrategyListViewController.swift
//  HWanGame
//
//  Created by Apple on 2024/8/13.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView

class HWStrategyListViewController: HWBaseViewController {

    private var topView = HWStrategyListTopView()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(HWStrategyListView.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 272*widthScale
        return tableView
    }()
    
    private var moreView = HWJoinedStrategyView()
    var httpManager = HWGameCommunityHttpManager()
    var attentionModels:[HWGameCommunityAllListModel] = []
    var models:[HWGameCommunityAllListModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = "#F7F8FA".toColor
        view.subviews{
            topView
            tableView
        }
        
        loadData()
        
        if #available(iOS 15.0, *) {
            self.tableView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }

        topView.leading(0).trailing(0).top(0).height(82*widthScale)
        tableView.leading(0).trailing(0).bottom(0).top(85*widthScale)

        moreView.frame = CGRect(x: 0, y: 0, width: .screenWidth, height: .screenHeight)
        currentWindow?.rootViewController?.view.addSubview(moreView)
        moreView.isHidden = true
        
        addRefresh()
        
        topView.toStrategyDetailVC = { detailID in
            let community = HWGameCommunityController()
            community.communityId = detailID
            self.navigationController?.pushViewController(community, animated: true)
        }
        topView.moreBlock = { dataArr in
            var tempArray = dataArr
            tempArray.remove(at: 0)
            let md = HWGameCommunityAllListModel()
            md.isLocal = true
            md.icon = "Strategy_add"
            md.name = "添加"
            md.id = "001000"
            tempArray.insert(md, at: dataArr.count - 1)
            self.moreView.sourceData = tempArray
            self.moreView.isHidden = false
        }
        
        moreView.removeView = {
            self.moreView.isHidden = true
        }
        
        moreView.toStrategyDetailVC = { communityId in
            self.moreView.isHidden = true
            if communityId == "001000" {
                self.navigationController?.pushViewController(HWJoinedMoreStrategyVC(), animated: true)
            }else{                
                let community = HWGameCommunityController()
                community.communityId = communityId
                self.navigationController?.pushViewController(community, animated: true)
            }            
        }
    }
    
}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension HWStrategyListViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return models.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HWStrategyListView", for: indexPath) as! HWStrategyListView
        cell.model = models[indexPath.row]
        cell.joinStrategy = { id in
            self.httpManager.gameCommunityFollow(id: id) { message in
                self.loadData()
            } failure: { errorString in
                self.view.showHUD(title: errorString)
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let community = HWGameCommunityController()
        community.communityId = models[indexPath.row].id
        self.navigationController?.pushViewController(community, animated: true)
    }

}

//MARK: data
extension HWStrategyListViewController {
    
    func loadData() -> Void {
        tableView.mj_footer?.isHidden = true
        httpManager.getGameCommunityAllList(type: "", keyword: "") { models in
            self.models.removeAll()
            self.attentionModels.removeAll()
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            let firstM = HWGameCommunityAllListModel()
            firstM.name = "推荐论坛"
            firstM.isLocal = true
            firstM.isSel = true
            firstM.icon = "Luntan_recommend"
            self.attentionModels.append(firstM)
            for m in models {
                if m.is_guanzhu {
                    self.attentionModels.append(m)
                }
                self.models.append(m)
            }
            self.topView.sourceData = self.attentionModels
            self.tableView.reloadData()
        } failure: { errorString in
            self.models.removeAll()
            self.tableView.reloadData()
            self.tableView.mj_footer?.isHidden = true
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
        }

    }
    
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
        tableView.mj_footer?.isHidden = true
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        self.tableView.mj_footer?.endRefreshing()
        httpManager.getGameCommunityAllListFooter(type: "", keyword: "") { models in
            self.tableView.mj_footer?.endRefreshing()
            for m in models {
                self.models.append(m)
            }
            self.tableView.reloadData()
        } failure: { errorString in
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
}

extension HWStrategyListViewController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
